package com.vortex.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.vortex.bean.AdminBean;
import com.vortex.dao.AdminDao;
import com.vortex.dao.BookDao;

/**
 * Servlet implementation class borrowServlet
 */
@WebServlet("/borrowServlet")
public class borrowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public borrowServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		//设置编码类型
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");

		BookDao bookdao = new BookDao();
		AdminDao admindao = new AdminDao();

		HttpSession session = request.getSession();
		//获取session设置的读者id
		String aid = (String)session.getAttribute("aid");
		//获取username读者姓名
//		String username = (String)request.getParameter("username");
		//通过aid获取到读者的信息
		AdminBean admin = admindao.get_AidInfo2(aid);
		//获取该读者当前的还能借阅的数量
		int max_num = admin.getMax_num();
		//获取图书数量
		int num = Integer.parseInt(request.getParameter("num"));
		//获取图书id
		int bid = Integer.parseInt(request.getParameter("bid"));
		//为了区分借书和还书的功能，设置tip，tip为1，表示借书
		int tip = Integer.parseInt(request.getParameter("tip"));

		//判断当前读者是否还能借书
		if (tip == 1 && max_num>0 && num>0) {
			//该图书的数量减少1
			num--;
			//将借阅记录存入数据表
			bookdao.borrowBook(bid, admin);
			bookdao.updateBookNum(bid, num);
			//该读者的可借阅数量减1
			max_num--;
			//更新该读者的可借阅数量
			//admin.setMax_num(max_num);
//			admindao.updateUser(Integer.parseInt(aid), admin.getUsername(), admin.getPassword(), admin.getName(),
//								admin.getEmail(), admin.getPhone(), admin.getLend_num(), max_num);
//			String beanaid = request.getParameter("beanaid");
			admindao.updateUserMax_numByAid(Integer.parseInt(aid), max_num);
			response.sendRedirect("/books/select.jsp");
		}
		else if(tip == 1 && (max_num<=0 || num<=0)){
			PrintWriter out = response.getWriter();
			out.print("<meta   http-equiv='Content-Type'   content='text/html;   charset=GBK'>");
			out.print("<script>");
			if(max_num<=0) {
				out.print("alert('当前您的借阅数量已经达到上限，请在归还部分书籍后再进行借阅!');");
                out.print("window.location.href='/books/borrow.jsp'");
			}
			if(num<=0){
				out.print("alert('当前该图书已经全部被借走，请稍后再进行借阅!');");
                out.print("window.location.href='/books/select.jsp'");
			}

			//response.sendRedirect("/books/borrow.jsp");
			out.print("</script>");
			out.close();
		}
		else {
			//还书功能，获取借阅记录的hid和aid(由于和session中设置的重名，因此命名为beanaid)
			int hid = Integer.parseInt(request.getParameter("hid"));
			String beanaid = request.getParameter("beanaid");
			//该图书的数量加1
			num++;
			//将借阅记录存入数据表
			//bookdao.borrowBook(bid, admin, num);
			bookdao.updateBookNum(bid, num);
			//通过aid获取到读者的信息
			AdminBean beanadmin = admindao.get_AidInfo2(beanaid);
			//获取该读者当前的还能借阅的数量
			int beanmax_num = beanadmin.getMax_num();
			//该读者的可借阅数量加1
			beanmax_num++;
			//更新该读者的可借阅数量
			//admin.setMax_num(max_num);
//			admindao.updateUser(Integer.parseInt(aid), admin.getUsername(), admin.getPassword(), admin.getName(),
//					admin.getEmail(), admin.getPhone(), admin.getLend_num(), max_num);
			admindao.updateUserMax_numByAid(Integer.parseInt(beanaid), beanmax_num);
//			admindao.updateUserMax_numByUsername(username, max_num);
			/**
			 * 还书在管理员和读者界面都有，为了区分，设置了show字段，show为1表示读者界面
			 */
			int show = Integer.parseInt(request.getParameter("show"));
			//调用还书函数，改变status字段
			bookdao.borrowBook2(hid);
			if (show == 1) {
				response.sendRedirect("/books/borrow.jsp");
			} else {
				response.sendRedirect("/books/admin_borrow.jsp");
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
